import { message } from 'antd';

import api from '../../api';

export default {
  namespace: 'yieldPerformanceM',
  state: {
    timeData: [],
    infoData: { dailyRate: { yield: null }, pastRate: { yield: null } },
    PortfolioInfo: {
      // 组合信息
      productYield: {},
      portfolioYield: {},
      yieldList: [],
    },
    constitute: [], // 产品组构成
    RankingData: {},
    echartsOne: { xAxis: undefined, series: [] },
    echartsTwo: { xAxis: undefined, series: [] },
    echartsThree: { xAxis: undefined, series: [] },
    echartsFour: { series: undefined },
    TableData: {},
    // TableData: { columns: [], dataSource: undefined },
    dimensionDisassembly: {},
  },

  effects: {
    //  获取时间区间的可选时间范围
    *findProductTime({ payload, callback }, { call, put }) {
      const res = yield call(api.profitAndLoss.findProductTime, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            timeData: res,
          },
        });
        callback(res);
      } else {
        message.error(res?.msg);
      }
    },

    // 获取时点信息
    *getInfo({ payload }, { call, put }) {
      const res = yield call(api.yieldPerformance.getInfo, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            infoData: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            infoData: { dailyRate: { yield: null }, pastRate: { yield: null } },
          },
        });
        message.error(res?.msg);
      }
    },

    // 获取第一个echarts数据
    *getEchartsOne({ payload }, { call, put }) {
      let res;
      if (payload.echartType == '2') {
        res = yield call(api.yieldPerformance.getEchartsOneYear, payload);
      } else {
        res = yield call(api.yieldPerformance.getEchartsOne, payload);
      }
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            echartsOne: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            echartsOne: { xAxis: undefined, series: [] },
          },
        });
        message.error(res?.message || res?.msg);
      }
    },
    // 获取第二个echarts数据
    *getEchartsTwo({ payload }, { call, put }) {
      let res;
      if (payload.echartType == '2') {
        res = yield call(api.yieldPerformance.getEchartsTwoYear, payload);
      } else {
        res = yield call(api.yieldPerformance.getEchartsTwo, payload);
      }
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            echartsTwo: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            echartsTwo: { xAxis: [], series: [] },
          },
        });
        message.error(res?.message || res?.msg);
      }
    },
    // 获取第三个echarts数据
    *getEchartsThree({ payload }, { call, put }) {
      const res = yield call(api.yieldPerformance.getEchartsThree, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            echartsThree: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            echartsThree: { xAxis: undefined, series: [] },
          },
        });
        message.error(res?.msg);
      }
    },

    // 获取第四个echarts数据
    *getEchartsFour({ payload }, { call, put }) {
      const res = yield call(api.yieldPerformance.getEchartsFour, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            echartsFour: res?.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            echartsFour: { series: undefined },
          },
        });
        message.error(res?.msg);
      }
    },
    // 获取排名
    *getRanking({ payload }, { call, put }) {
      const res = yield call(api.yieldPerformance.getRanking, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            RankingData: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            RankingData: {},
          },
        });
        message.error(res?.msg);
      }
    },
    // 获取表格数据
    *getTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.yieldPerformance.getTableData, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            TableData: res.data,
          },
        });
        if (res.msg == ' 给定区间缺少组合的持仓交易数据') {
          message.warning('该区间内无持仓交易数据，请重新选择时间区间');
        }
        callback(res.data);
      } else {
        message.error(res.msg);
        callback();
      }
    },
    // 获取大类资产收益率维度拆解数据
    *getDimensionDisassembly({ payload, callback }, { call, put }) {
      const res = yield call(api.yieldPerformance.getDimensionDisassembly, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            dimensionDisassembly: res.data,
          },
        });
        callback(res.data);
      } else {
        message.error(res.msg);
        callback();
      }
    },

    // 获取时点信息
    *getPortfolioInfo({ payload }, { call, put }) {
      const res = yield call(api.yieldPerformance.getPortfolioInfo, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            PortfolioInfo: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            PortfolioInfo: {
              // 组合信息
              productYield: {},
              portfolioYield: {},
              yieldList: [],
            },
          },
        });

        message.error(res?.msg);
      }
    },
    // 获取时点信息
    *getGroupInfo({ payload }, { call, put }) {
      const res = yield call(api.yieldPerformance.getGroupInfo, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            constitute: res.data,
          },
        });
      } else {
        message.error(res?.msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
